//package work.shyk.license;
//
//import sun.security.tools.keytool.CertAndKeyGen;
//import sun.security.x509.X500Name;
//
//import java.io.FileOutputStream;
//import java.io.IOException;
//import java.security.InvalidKeyException;
//import java.security.KeyStore;
//import java.security.KeyStoreException;
//import java.security.NoSuchAlgorithmException;
//import java.security.NoSuchProviderException;
//import java.security.PrivateKey;
//import java.security.SignatureException;
//import java.security.cert.CertificateException;
//import java.security.cert.X509Certificate;
//import java.util.Date;
//
///**
// * description: <br>
// * createTime: 2021/5/17 11:27 <br>
// *
// * @author zzh
// */
//public class KeyStoreCreate {
//    public static String filePath = "D:\\licenseTest\\test\\";
//    private static final int keysize = 1024;
//    private static final String commonName = "pcitc.com";
//    private static final String organizationalUnit = "IT";
//    private static final String organization = "pcitc";
//    private static final String city = "nanjing";
//    private static final String state = "nanjing";
//    private static final String country = "nanjing";
//    private static final long validity = 1096; // 3 years
//    private static final String privateAlias = "privateKey";
//    private static final String publicAlias = "publicKey";
//    private static final char[] keyPassword = "123456".toCharArray();
//
//    public static void main(String[] args) {
//        try {
//
//            KeyStore privateKs = KeyStore.getInstance("pkcs12");
//            privateKs.load(null, null);
//
//            CertAndKeyGen keypair = new CertAndKeyGen("DSA", "SHA1WithRSA", null);
//            X500Name x500Name = new X500Name(commonName, organizationalUnit, organization, city, state, country);
//            keypair.generate(keysize);
//
//            PrivateKey privateKey = keypair.getPrivateKey();
//            X509Certificate[] chain = new X509Certificate[1];
//            chain[0] = keypair.getSelfCertificate(x500Name, new Date(), (long)validity*24*60*60);
//
//            // store away the key store
//            FileOutputStream privateFos = new FileOutputStream(filePath + "private.keystore");
//            privateKs.setKeyEntry(privateAlias, privateKey, keyPassword, chain);
//            privateKs.store(privateFos, keyPassword);
//            privateFos.close();
//
//
//            KeyStore publicKs = KeyStore.getInstance("pkcs12");
//            publicKs.load(null, null);
//            //PublicKey publicKey = keypair.getPublicKey();
//            //X509Certificate[] publicChain = new X509Certificate[1];
//            //publicChain[0] = keypair.getSelfCertificate(x500Name, new Date(), (long)validity*24*60*60);
//            //
//            //// store away the key store
//
//            FileOutputStream publicFos = new FileOutputStream(filePath + "public.keystore");
//            privateKs.setCertificateEntry(publicAlias, chain[0]);
//            privateKs.store(publicFos, keyPassword);
//            publicFos.close();
//
//            //CertAndKeyGen gen = new CertAndKeyGen("RSA","SHA1WithRSA");
//            //gen.generate(1024);
//            //
//            //X509Certificate cert=gen.getSelfCertificate(new X500Name("CN=ROOT"), (long)365*24*3600);
//            //
//            //publicKs.setCertificateEntry("cert", cert);
//            //publicKs.store(publicFos, "123456".toCharArray());
//            System.out.println("create Success");
//        } catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException | NoSuchProviderException | InvalidKeyException | SignatureException e) {
//            e.printStackTrace();
//        }
//    }
//}
